package ms07.ticketPro.activities;

import ms07.ticketPro.DebugLog;
import ms07.ticketPro.R;
import ms07.ticketPro.data.Stations;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.RadioButton;

public class ActivityTicketRequest extends Activity {

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);  
		this.setContentView(R.layout.request_ticket_buy);
		
		
		//now set right adapters to auto complete from and to
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, Stations.NAMES);
		
		AutoCompleteTextView from = (AutoCompleteTextView)this.findViewById(R.id.ticket_create_from);
		AutoCompleteTextView to = (AutoCompleteTextView)this.findViewById(R.id.ticket_create_to);
		
		from.setAdapter(adapter);
		to.setAdapter(adapter);
		
		
		RadioButton adult_1 = (RadioButton)this.findViewById(R.id.ticket_create_adult_1);
		RadioButton child_0 = (RadioButton)this.findViewById(R.id.ticket_create_child_0);
		
		adult_1.setChecked(true);
		child_0.setChecked(true);
		
		Button send_sms = (Button)this.findViewById(R.id.ticket_create_contiune);
		
		send_sms.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	                // Perform action on click
	            	ActivityTicketRequest.this.prepareSMS();
	            }
	        });
	}
	
	protected int getAdults(){
		RadioButton adult;

		adult = (RadioButton)this.findViewById(R.id.ticket_create_adult_1);
		if(adult.isChecked()){
			return 1;
		}
		adult = (RadioButton)this.findViewById(R.id.ticket_create_adult_2);
		if(adult.isChecked()){
			return 2;
		}
		adult = (RadioButton)this.findViewById(R.id.ticket_create_adult_3);
		if(adult.isChecked()){
			return 3;
		}
		
		return 0;
	}
	
	protected int getChildren(){
		RadioButton child;
		
		child = (RadioButton)this.findViewById(R.id.ticket_create_child_1);
		if(child.isChecked()){
			return 1;
		}
		
		child = (RadioButton)this.findViewById(R.id.ticket_create_child_2);
		if(child.isChecked()){
			return 2;
		}
		
		return 0;
	}
	
	protected void prepareSMS(){
		//build sms :)
		
		DebugLog.getInstance().log("start PREPARE sms!");
		
		/**
		 * SMS LAYOUT:
		 * 
		 * ###### START ######
		 * zug
		 * Abfahrtsbahnhof*
		 * Zielbahnhof*
		 * Vor- und Nachname*
		 * xErw (Anzahl Erwachsene)
		 * xK (Anzahl Kinder)
		 * VORTEILScard
		 * x.KL (1. oder 2. Klasse)
		 * Reisedatum (TT.MM.)
		 * ###### END ########
		 */
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		AutoCompleteTextView from = (AutoCompleteTextView)this.findViewById(R.id.ticket_create_from);
		AutoCompleteTextView to = (AutoCompleteTextView)this.findViewById(R.id.ticket_create_to);
		int children = this.getChildren();
		int adults = this.getAdults();
		String seatclass = prefs.getString("seatclass", "2");
		String username = prefs.getString("username", "John Doe");
		
		String SEP = " ";
		
		String sms = new String();
		
		sms+= "zug"+SEP;
		sms+= from.getText()+SEP;
		sms+= to.getText()+SEP;
		sms+= username+SEP;
		if(children>0){
			sms+= Integer.toString(adults)+"Erw"+SEP;
			sms+= Integer.toString(children)+"K"+SEP;
		}else if(adults>1){
			sms+= Integer.toString(adults)+"Erw"+SEP;
		}
		if(!seatclass.equals("2")){
			sms+= seatclass+".KL"+SEP;
		}
		
		DebugLog.getInstance().log("##### SMS START #####");
		DebugLog.getInstance().log(sms);
		DebugLog.getInstance().log("##### SMS END #####");
		
		SmsManager sender = SmsManager.getDefault(); 
		sender.sendTextMessage(prefs.getString("smsprovider", "082820200"),null, sms,null, null);
	}
}
